package com.yogee.topseller.activity.my;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.bean.ViewInformationBean;
import com.yogee.topseller.fragment.my_order.ProductReservationFragment;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.TabLayoutUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyOrderActivity extends HttpActivity {
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.img_cursor)
    ImageView img_cursor;
    @BindView(R.id.layout)
    LinearLayout layout;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    private String type;

    private int offset = 0;
    private int currIndex = 0;
    private int bmpW;
    
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 动态赋值
        if(titles.size()>0)
            getNum();
    }

    @Override
    protected void initView() {
        title.setTitle("加入会员");
        title.setActivity(this);
        //确定当前显示那个界面
        type = getIntent().getStringExtra("title");//0.产品预约  1.打款凭证  2.回寄合同  3.佣金结算
        if(type==null||type.equals("")){
            type = "0";
        }

        titles.add("产品预约");
        titles.add("打款凭证");
        titles.add("回寄合同");
        titles.add("佣金结算");
        for (int i = 0; i < 4; i++) {
            ProductReservationFragment reservationFragment = new ProductReservationFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type", i);
            reservationFragment.setArguments(bundle);
            fragments.add(reservationFragment);
        }

        initCursor(4);
        new TabLayoutUtil(getSupportFragmentManager(), layout, fragments, titles, 101, type);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            int one, two,three;

            @Override
            public void onPageSelected(int position) {
                one = offset * 2 + bmpW;
                two = one * 2;
                three = one*3;
                Animation animation = null;
                if(currIndex==0){
                    animation = new TranslateAnimation(0, 0, 0, 0);
                }else if (currIndex==1){
                    animation = new TranslateAnimation(0, one, 0, 0);
                }else if(currIndex == 2){
                    animation = new TranslateAnimation(one, two, 0, 0);
                }else if(currIndex == 2){
                    animation = new TranslateAnimation(two, three, 0, 0);
                }
                switch (position) {
                    case 0:
                        if (currIndex == 1) {
                            animation = new TranslateAnimation(one, 0, 0, 0);
                        } else if (currIndex == 2) {
                            animation = new TranslateAnimation(two, 0, 0, 0);
                        } else if(currIndex == 3) {
                            animation = new TranslateAnimation(three, 0, 0, 0);
                        }
                        break;
                    case 1:
                       if (currIndex == 0) {
                            animation = new TranslateAnimation(offset, one, 0, 0);
                        } else if (currIndex == 2) {
                            animation = new TranslateAnimation(two, one, 0, 0);
                        } else if (currIndex == 3) {
                           animation = new TranslateAnimation(three, one, 0, 0);
                       }
                        break;
                    case 2:
                        if (currIndex == 0) {
                            animation = new TranslateAnimation(offset, two, 0, 0);
                        } else if (currIndex == 1) {
                            animation = new TranslateAnimation(one, two, 0, 0);
                        }else if (currIndex == 3) {
                            animation = new TranslateAnimation(three, two, 0, 0);
                        }
                        break;
                    case 3:
                        if (currIndex == 0) {
                            animation = new TranslateAnimation(offset, three, 0, 0);
                        } else if (currIndex == 1) {
                            animation = new TranslateAnimation(one, three, 0, 0);
                        }else if (currIndex == 2) {
                            animation = new TranslateAnimation(two, three, 0, 0);
                        }
                        break;
                }
                currIndex = position;

                animation.setFillAfter(true);
                animation.setDuration(300);
                img_cursor.startAnimation(animation);


            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        getNum();
    }

    void initCursor(int size){

        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.cursor                           )
                .getWidth();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        int imgWidth = AppUtil.dip2px(MyOrderActivity.this, 0);
        screenW = screenW-imgWidth;
        offset = (screenW/size-bmpW)/2;
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        img_cursor.setImageMatrix(matrix);

        Animation animation = null;
        int one = offset * 2 + bmpW;
        int two = one * 2;
        int three = one*3;
        switch(type){
            case "0":
                animation = new TranslateAnimation(0, 0, 0, 0);
                break;
            case "1":
                animation = new TranslateAnimation(0, one, 0, 0);
                break;
            case "2":
                animation = new TranslateAnimation(one, two, 0, 0);
                break;
            case "3":
                animation = new TranslateAnimation(two, three, 0, 0);
                break;

        }
        currIndex = Integer.valueOf(type);

        animation.setFillAfter(true);
        animation.setDuration(0);
        img_cursor.startAnimation(animation);

    }

    public void getNum(){
        HttpManager.getInstance().viewInformation(AppUtil.getUserId(MyOrderActivity.this)).subscribe(new BaseSubscriber(new HttpOnNextListener<ViewInformationBean>() {
            @Override
            public void onNext(ViewInformationBean o) {
                for(int i = 0;i<titles.size();i++){
                    TabLayout.Tab tabs = tab.getTabAt(i);//获得每一个tab
                    TextView num = (TextView) tabs.getCustomView().findViewById(R.id.tab_item_num_tv);
                    if(i==0){
                        if(!"0".equals(o.getCountI())){
                            num.setText(o.getCountI());
                            num.setVisibility(View.VISIBLE);
                        }else{
                            num.setVisibility(View.GONE);
                        }

                    }else if(i==1){
                        if(!"0".equals(o.getCountII())){
                            num.setText(o.getCountII());
                            num.setVisibility(View.VISIBLE);
                        }else{
                            num.setVisibility(View.GONE);
                        }

                    }else if(i==2){
                        if(!"0".equals(o.getCountIII())){
                            num.setText(o.getCountIII());
                            num.setVisibility(View.VISIBLE);
                        }else{
                            num.setVisibility(View.GONE);
                        }

                    }else if(i==3){
                        if(!"0".equals(o.getCountIV())){
                            num.setText(o.getCountIV());
                            num.setVisibility(View.VISIBLE);
                        }else{
                            num.setVisibility(View.GONE);
                        }

                    }else{
                        num.setVisibility(View.GONE);
                    }

                }

            }
        }, this));
    }
    
}
